USE [NGroup.VTL.DB]
GO

/****** Object:  UserDefinedFunction [dbo].[SplitString]    Script Date: 12/25/2013 22:45:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SplitString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SplitString]
GO

USE [NGroup.VTL.DB]
GO

/****** Object:  UserDefinedFunction [dbo].[SplitString]    Script Date: 12/25/2013 22:45:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[SplitString]
(
 @string varchar(max),
 @delimiter char(1) = ','
)
RETURNS @temptable TABLE(Item varchar(50))
AS
BEGIN
 DECLARE @NextString NVARCHAR(40)
 DECLARE @Pos INT
 DECLARE @NextPos INT
 
 --SET @delimiter = ','
 SET @string = @string + @delimiter
 SET @Pos = charindex(@delimiter,@string)

 WHILE (@pos <> 0)
 BEGIN
  SET @NextString = substring(@string,1,@Pos - 1)
  IF (@NextString IS NOT NULL AND @NextString <> '')
   INSERT INTO @temptable(Item) VALUES(@NextString)
  SET @string = substring(@string,@pos+1,len(@string))
  SET @pos = charindex(@delimiter,@string)
 END
 RETURN
END
GO

